<?php

defined('BASEPATH') or exit('No direct script access allowed');

/**
 * Description of Person
 *
 * @author doufu <442958506@qq.com>
 */
class Person extends REST_Member_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->library('form_validation');
		$this->load->model('api/User_model', 'users');
		$this->load->model('api/Orderadd_model', 'orderAdd');
		$this->load->model('api/System_model', 'system');
		$this->load->model('api/Areas_model', 'areas');
	}

	/**
	 * 重置密码
	 */
	public function reset() {
		$this->form_validation->validateData(array(
			'password',
			'newPassword'
				), array(
			array(
				'password',
				'required|min_length[6]|validate_old_password[user.username]'
			),
			array(
				'newPassword',
				'required|min_length[6]|differs[password]'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$data = array(
			'password_hash' => md5(md5($this->form_validation->newPassword))
		);
		$result = $this->users->update($data, array(
			'id' => $this->identity ['uid']
		));
		if ($result) {
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	/**
	 * 设置地区
	 */
	public function setAreas() {
		$this->form_validation->validateData(array(
			'address_id'
				), array(
			array(
				'address_id',
				'required|integer|exist[areas_enabled.id]'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}
		$data ['address_id'] = $this->form_validation->address_id;
		$result = $this->users->update($data, array(
			'id' => $this->identity ['uid']
		));
		if ($result) {
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	/**
	 * 新增收获地址
	 */
	public function addAddress() {
		$this->form_validation->validateData(array(
			'address',
			'sex',
			'contact',
			'tel',
			'areasid'
				), array(
			array(
				'address',
				'required|min_length[0]'
			),
			array(
				'sex',
				'numeric'
			),
			array(
				'contact',
				'required|min_length[0]'
			),
			array(
				'tel',
				'numeric'
			),
			array(
				'areasid',
				'numeric'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$description = $this->areas->getAreasAll2($this->form_validation->areasid);

		$data = array(
			'user_id' => $this->identity['uid'],
			'address' => $this->form_validation->address,
			'sex' => $this->form_validation->sex,
			'contactName' => $this->form_validation->contact,
			'areas_id' => $this->form_validation->areasid,
			'tel' => $this->form_validation->tel,
			'description' => $description['description'] . ',' . $description['address'] . ',' . $this->form_validation->address
		);

		if ($this->orderAdd->add($data)) {
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	/**
	 * 取得对应的用户所有地址信息
	 */
	public function getAddress() {
		$data = $this->orderAdd->getAddressAll($this->identity['uid']);		
		$this->renderJson(11100, array('data' => array_values($data)));
	}

	/**
	 * 取得单条地址（编辑用）
	 */
	public function getAddressOne() {
		$this->form_validation->validateData(array(
			'id',
			'areas_id'
				), array(
			array(
				'id',
				'greater_than[0]'
			),
			array(
				'areas_id',
				'greater_than[0]'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		$a = $this->orderAdd->findAddByArsid($this->form_validation->areas_id);
		if (empty($a)) {
			$this->renderJson(11200);
		} else {
			$data['data'] = $this->orderAdd->getAddAllByid($this->form_validation->id);

			$data2['data'] = array(
				'id' => $data['data']['id'],
				'tel' => $data['data']['tel'],
				'contactName' => $data['data']['contactName'],
				'user_id' => $data['data']['user_id'],
				'sex' => $data['data']['sex'],
				'areas_id' => $data['data']['areas_id'],
				'address' => $data['data']['address'],
				'description' => $data['data']['description'] . ',' . $data['data']['address2'],
				'addressShort' => $data['data']['address2']
			);

			if (!empty($data['data'])) {
				$this->renderJson(11100, $data2);
			}
		}
		$this->renderJson(11200);
	}

	/**
	 * 删除单条地址
	 */
	public function deAddress() {
		$this->form_validation->validateData(array(
			'id'
				), array(
			array(
				'id',
				'greater_than[0]'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}

		if ($this->orderAdd->deleteAgent($this->form_validation->id)) {
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	/**
	 * 更新一个地址 
	 */
	public function editAddress() {
		$this->form_validation->validateData(array(
			'id',
			'address',
			'sex',
			'contactName',
			'tel',
			'areasid'
				), array(
			array(
				'id',
				'greater_than[0]'
			),
			array(
				'address',
				'required|min_length[0]'
			),
			array(
				'sex',
				'numeric'
			),
			array(
				'contactName',
				'required|min_length[0]'
			),
			array(
				'tel',
				'numeric'
			),
			array(
				'areasid',
				'numeric'
			)
		));

		if ($this->form_validation->hasErrors()) {
			$data ['error'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}


		$description = $this->areas->getAreasAll2($this->form_validation->areasid);

		$data = array(
			'address' => $this->form_validation->address,
			'sex' => $this->form_validation->sex,
			'contactName' => $this->form_validation->contactName,
			'tel' => $this->form_validation->tel,
			'description' => $description['description'] . ',' . $description['address'] . ',' . $this->form_validation->address
		);

		if ($this->orderAdd->updateAddress($data, $this->form_validation->id)) {
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	/**
	 * 获取联系我们信息
	 */
	public function getContact() {
		$data['data'] = $this->system->findSystemInfo();
		if (!empty($data)) {
			$this->renderJson(11100, $data);
		}
		$this->renderJson(11200);
	}

	public function addComment() {
		$this->form_validation->validateData(array(
			'orderId', 'product_score', 'schedule_score', 'service_score', 'content',
				), array(
			array(
				'orderId', 'required|integer|greater_than[0]'
			),
			array(
				'product_score', 'required|integer|greater_than_equal_to[0]|less_than_equal_to[5]'
			),
			array(
				'schedule_score', 'required|integer|greater_than_equal_to[0]|less_than_equal_to[5]'
			),
			array(
				'service_score', 'required|integer|greater_than_equal_to[0]|less_than_equal_to[5]'
			),
			array(
				'content', 'required'
			)
		));
		if ($this->form_validation->hasErrors()) {
			$data['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}
		$this->load->config('system_info');
		$cfg_filter = explode('|', config_item('cfg_filter'));
		$data = array(
			'order_id' => $this->form_validation->orderId,
			'user_id' => $this->identity['uid'],
			'product_score' => $this->form_validation->product_score,
			'schedule_score' => $this->form_validation->schedule_score,
			'service_score' => $this->form_validation->service_score,
			'content' => json_encode(str_replace($cfg_filter, '**', $this->form_validation->content)),
			'created_at' => time()
		);
		$this->load->model('api/Comment_model', 'Comment');
		$result = $this->Comment->addComment($data);
		if($result) {
			$this->renderJson(11100);
		}
		$this->renderJson(11200);
	}

	public function comment() {
		$this->form_validation->validateData(array(
			'orderId'
				), array(
			array(
				'orderId', 'required|integer|greater_than[0]'
			)
		));
		if ($this->form_validation->hasErrors()) {
			$data['msg'] = $this->form_validation->error;
			$this->renderJson(10005, $data);
		}
		$this->load->model('api/Comment_model', 'Comment');
		$this->page['limit'] = 8; //每页显示8条
		$data = $this->Comment->comment($this->form_validation->orderId, $this->identity['uid'], $this->page);
		if(!empty($data)) {
			foreach ($data as &$v) {
				$v['username'] = preg_replace('/(1\d{2})\d{4}(\d*)/i', '$1***$2', $v['username']);
				$v['content'] = json_decode($v['content']);
			}
		}
		$this->renderJson(11100, array('data'=>$data));
	}
	
	public function info() {
		$this->renderJson(11100, array('data'=>array(
			'balance' => '0.00',
			'credits' => '0'
		)));
	}
}
